﻿namespace D.WebApi.Core.Dtos
{

    /// <summary>API分页调用结果</summary>
    public class ApiPageResult<T> : ApiBaseResult
    {
        /// <summary>
        /// 当前页索引
        /// </summary>
        public long PageNo { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public long TotalPage { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public long TotalCount { get; set; }

        /// <summary>
        /// 每页的记录数
        /// </summary>
        public long PageSize { get; set; }

        /// <summary>
        /// 数据集
        /// </summary>
        public List<T> Data { get; set; }

        /// <summary>生成成功时的应答实例</summary>
        /// <param name="data">应答数据</param>
        /// <param name="totalPage"></param>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiPageResult<T> Success(T data, long totalPage, string message = "success")
        {
            return new ApiPageResult<T> { Code = 200, Data = null, TotalPage = totalPage, Message = message };
        }

        /// <summary>生成失败时的应答实例</summary>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiPageResult<T> Fail(string message = "fail")
        {
            return new ApiPageResult<T> { Code = 500, Data = null, TotalPage = 0, Message = message };
        }

        /// <summary>设置成功时的应答</summary>
        /// <param name="message">应答消息</param>
        /// <param name="data">应答数据</param>
        /// <param name="totalPage"></param>
        public void SetSuccess(string message, List<T> data, long totalPage = 0)
        {
            Code = 200;
            Message = message;
            Data = data;
            TotalPage = totalPage;
        }
    }

}
